package com.powernode.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.shop.domain.ProdProp;
import com.powernode.shop.domain.ProdPropValue;
import com.powernode.shop.service.ProdPropService;
import com.powernode.shop.service.ProdPropValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/26
 */
@RestController
@RequestMapping("/prod/spec")
public class ProdPropController {

    @Autowired
    private ProdPropService prodPropService;

    @Autowired
    private ProdPropValueService prodPropValueService;

    /**
     * 1、分页查询商品规格
     *
     * @param page
     * @param prodProp
     * @return
     */
    @PreAuthorize("hasAuthority('prod:spec:page')")
    @GetMapping("/page")
    public ResponseEntity<Page<ProdProp>> loadProdPropPage(Page<ProdProp> page, ProdProp prodProp) {
        Page<ProdProp> prodPropPage = prodPropService.selectProdPropPage(page, prodProp);
        return ResponseEntity.ok(prodPropPage);
    }

    /**
     * 2、新增规格
     *
     * @param prodProp
     * @return
     */
    @PreAuthorize("hasAuthority('prod:spec:save')")
    @PostMapping
    public ResponseEntity<Void> saveProdProp(@RequestBody ProdProp prodProp) {
        // 重写save
        prodPropService.save(prodProp);
        return ResponseEntity.ok().build();
    }

    /**
     * 3、新增商品：回显所有规格
     *
     * @return
     */
    @PreAuthorize("hasAuthority('prod:spec:page')")
    @GetMapping("/list")
    public ResponseEntity<List<ProdProp>> loadProdPropList() {
        List<ProdProp> prodPropList = prodPropService.list();
        return ResponseEntity.ok(prodPropList);
    }

    /**
     * 3、新增商品：回显：根据id 查出规格value
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('prod:spec:page')")
    @GetMapping("/listSpecValue/{id}")
    public ResponseEntity<List<ProdPropValue>> loadProdPropValueList(@PathVariable Long id) {
        List<ProdPropValue> prodPropValueList = prodPropValueService.list(new LambdaQueryWrapper<ProdPropValue>()
                .eq(ProdPropValue::getPropId, id)
        );
        return ResponseEntity.ok(prodPropValueList);
    }


}
